package org.example.salesystem.controller;

import org.example.salesystem.config.CodeGenerator;
import org.example.salesystem.entity.ResultBody;
import org.example.salesystem.service.RelProService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/ReleseProduction")
@CrossOrigin(origins="*")
public class RelProController {
    @Autowired
    private RelProService relProService;
//卖家添加商品
    @RequestMapping("/addProduct")
    public ResultBody addProduct(@RequestBody Map<String, Object> map) {
        // 判断参数是否完整
        if (map.get("name") == null || map.get("description") == null || map.get("price") == null ||
                map.get("category") == null || map.get("phone") == null||map.get("avatar_Url")==null) {
            return new ResultBody(404, "缺少参数", false);
        }
        String category = (String) map.get("category"); // 商品类别
        String name = (String) map.get("name"); // 商品名称
        String description = (String) map.get("description"); // 商品描述
        Object priceObj = map.get("price"); // 商品价格
        String phone = (String) map.get("phone"); // 联系电话
        String avatar_Url=(String) map.get("avatar_Url");//商品图片
        String code = CodeGenerator.generateId(); // 生成编号

        // 将价格转换为 Double 类型
        Double price;
        if (priceObj instanceof Integer) {
            price = ((Integer) priceObj).doubleValue();
        } else if (priceObj instanceof Double) {
            price = (Double) priceObj;
        } else {
            return new ResultBody(400, "价格格式错误", false);
        }
        int shopMessage = relProService.addProduct(code, category, name, description, price, phone,avatar_Url);
        // 判断是否传值成功
        if (shopMessage > 0) {
            // 增加成功
            return new ResultBody(200, "卖家发布成功", map);
        } else {
            // 增加失败
            return new ResultBody(400, "卖家发布失败", false);
        }
    }
    //显示商品信息（卖家端）
    @RequestMapping("/displayProduct")
    public ResultBody displayProduct(@RequestBody Map<String, Object> map) {
        //判断参数是否完整
        if (map.get("phone") == null) {
            return new ResultBody(404, "缺少参数", false);
        }
        String phone = (String) map.get("phone");
        //判断电话号码是否正确
        if(relProService.displayProduct(phone)==null)
        {
            return new ResultBody(400,"电话号码错误",false);
        }
        return relProService.displayProduct(phone);
    }

    //显示待上架商品信息(管理员端）
    @RequestMapping("/displayShopW")
    public ResultBody displayShop() {
        System.out.println("显示待上架商品信息");
        return relProService.getAllMessage();
    }
    //显示已上架商品信息（管理员端）
    @RequestMapping("/displayShopR")
    public ResultBody displayShopR() {
        return relProService.displayShopR();
    }
    //决策商品是否上架
    @RequestMapping("/decisionShop")
    public ResultBody decision(@RequestBody Map<String, Object> map) {
        if(map.get("decision")==null||map.get("code")==null)
        {//传入决策,商品编号
            return new ResultBody(404,"缺少参数",false);
        }
        Object object = map.get("decision");
        String decision;
        Object object1 = map.get("code");
        String code;
        if(object instanceof Integer)
        {
            decision=((Integer) object).toString();
        }
        else if(object instanceof Double)
        {
            decision=((Double) object).toString();
        }
        else if (object instanceof String)
        {
            decision=(String) object;
        }
        else
            return new ResultBody(404,"缺少参数",false);
        if(object1 instanceof Integer)
        {
            code=((Integer) object1).toString();
        }
        else if(object1 instanceof Double)
        {
            code=((Double) object1).toString();
        }
        else if (object1 instanceof String)
        {
            code=(String) object1;
        }
        else
            return new ResultBody(404,"缺少参数",false);
        return relProService.decision(decision,code);
    }
}   